<?php
/*
 * Created on Mar 7, 2009
 * author: David Palmer <blinder.dave@gmail.com>
 * 
 * The controller is the central script to route the various requests
 * our website will generate. The routing refers to invoking action classes
 * that handle our individual actions/requests. 
 * 
 * This system makes extensive use of PHP's reflection mechanism so that
 * we aren't coupling the front-end of the web site with what will handle
 * the actual requests. This basically follows the MVC design pattern (in
 * as much as MVC is a design pattern)
 * 
 * Another important note is that this system uses Smarty, the PHP templating
 * mechanism so that we can separate our business logic from presentation. 
 */

$path_extra = dirname(dirname(__FILE__)) . "/libs";
$path = ini_get('include_path');
$path = $path_extra . PATH_SEPARATOR . $path;
ini_set('include_path', $path);
 
include_once("config.php");
include_once("i_product.php");
include_once("c_tools.php");
include_once("c_container.php");
session_cache_limiter( 'nocache' );
// lets start our session
session_start();
// lets make sure our dependent class definitions are loaded
Tools::loadClassPath(CLASSPATH);
// lets initialize our cache system
$memcache = Tools::initCache();

// a hook to flush our memcache
if ($_SERVER['REQUEST_URI'] == "/_flush_") {
	$memcache->flush();
	@header("Location: /");
	die();
}

try {
	$container = new Container();
} catch (Exception $ex) {
	print("could not start up our container: ".$ex);
}
// lets get our config bean, we'll need
$appConfig = $container->getBean("appConfig");

$do = Tools::getDoParameter($_REQUEST['do']);

try { 
	if (($bean = $container->getBean($do))) {
			$product = $bean->execute($_REQUEST);
	} else if (($bean = $container->getBean("static-page"))) {
			$product = $bean->execute($_REQUEST);
	} else { 
		throw new Exception("error");
	}
} catch (Exception $ex) {
	$do = "fatal_error";
	$error_bean=$container->getBean($do);
	$product = $error_bean->execute("An error occured: " . $ex->getMessage());
}
$contentType = $product->getContentType();
$content=$product->getOutput();
$httpStatus = $product->getHttpStatus();
if($content!=""){
	@header('Content-Length ' . strlen($content));
	if($contentType!=""){
		@header("Content-type: " . $contentType);
	}
}
@header('HTTP/1.1 ' . $httpStatus);
print($content);
?>